#include "GunContra.h"


CGunContra::CGunContra()
{
	_timerF = CTimer(3000.0f);
	_timerL = CTimer(200.0f);
	_countF = 0;
}

void CGunContra::GunContra_Run(bool _isJump, bool _isRight, int _isDown, bool _isMove, int _levelBullet, float _x, float _y, CSprite *_rSprite, CSprite *_lSprite)
{
	float vx = 0, vy = 0;
	float idx = 0, idy = 0;

	#pragma region vx,vy bullet
	#pragma region no jump
		if (!_isJump)
		{
			if (_isRight)
			{

				if (_isDown == 1) // nam
				{
					if (_isMove)//chia sung xeo xuong
					{
						vx = 1.0f;
						vy = -1.0f;
						idx = 0;
						idy = -FRAMEWIDTH / 2;
					}
					else   //nam
					{
						vx = 1.0f;
						vy = 0.0f;
						idx = 0;
						idy = -FRAMEWIDTH / 2;
					}
				}
				else
				{
					if (_isDown == -1)	 //len
					{
						if (_isMove)   //chia sung xeo len
						{
							vx = 1.0f;
							vy = 1.0f;
							idx = 0;
							idy = FRAMEWIDTH / 2 - 10;
						}
						else   //chia sung len
						{
							vx = 0.0f;
							vy = 1.0f;
							idx = -FRAMEHEIGHT / 2 + 10;// -CONTRAWIDTH / 2 + 5;
							idy = CONTRAWIDTH / 2;
							_rSprite->Next(8, 2);
						}
					}
					else
					{
						if (_isMove)//chay ban
						{
							vx = 1.0f;
							vy = 0.0f;
							idx = 0;
							idy = 0;
							_rSprite->Next(30, 5);
						}
						else  //dung yen
						{
							vx = 1.0f;
							vy = 0.0f;
							idx = -16;
							idy = -3;
							_rSprite->Next(5, 2);
						}
					}
				}
			}
			else
			{
				if (_isDown == 1)
				{
					if (_isMove) // chia sung xuong
					{
						vx = -1.0f;
						vy = -1.0f;
						idx = -FRAMEHEIGHT;
						idy = -FRAMEWIDTH / 2;
					}
					else
					{
						vx = -1.0f;
						vy = 0.0f;
						idx = -FRAMEHEIGHT;
						idy = -FRAMEWIDTH / 2;
					}
				}
				else
				{
					if (_isDown == -1)
					{
						if (_isMove)	//chia sung len
						{
							vx = -1.0f;
							vy = 1.0f;
							idx = -FRAMEHEIGHT + 20;
							idy = FRAMEWIDTH / 2 - 12;
						}
						else
						{
							vx = 0.0f;
							vy = 1.0f;
							idx = -FRAMEHEIGHT / 2 + 4;
							idy = FRAMEWIDTH / 2 + 10;
							_lSprite->Next(0, 2);
						}
					}
					else
					{
						if (_isMove)//chay ban
						{
							vx = -1.0f;
							vy = 0.0f;
							idx = -FRAMEHEIGHT;
							idy = 0;
							_lSprite->Next(35, 5);
						}
						else   // dung yen chia sung ra
						{
							vx = -1.0f;
							vy = 0.0f;
							idx = -FRAMEHEIGHT;
							idy = -3;
							_lSprite->Next(3, 2);
						}
					}
				}
			}
		}
	#pragma endregion
		else
		{
			if (_isRight)
			{
				if (_isDown == -1)		//len
				{
					if (_isMove)
					{
						vx = 1.0f;
						vy = 1.0f;
						idx = -FRAMEHEIGHT / 2 + 25;
						idy = -FRAMEWIDTH / 2 + 10;
					}
					else
					{
						vx = 0.0f;
						vy = 1.0f;
						idx = -FRAMEHEIGHT / 2 + 5;
						idy = -FRAMEWIDTH / 2 + 30;
					}
				}
				else
				{
					if (_isDown == 1)
					{
						if (_isMove)
						{
							vx = 1.0f;
							vy = -1.0f;
							idx = -FRAMEHEIGHT / 2 + 20;
							idy = -FRAMEWIDTH + 15;
						}
						else
						{
							vx = 0.0f;
							vy = -1.0f;
							idx = -FRAMEHEIGHT / 2 + 5;
							idy = -FRAMEWIDTH / 2 - 15;
						}
					}
					else
					{
						vx = 1.0f;
						vy = 0.0f;
						idx = -FRAMEHEIGHT / 2 + 25;
						idy = -FRAMEWIDTH / 2;
					}
				}
			}
			else
			{
				if (_isDown == -1)		//len
				{
					if (_isMove)
					{
						vx = -1.0f;
						vy = 1.0f;
						idx = -FRAMEHEIGHT + 15;
						idy = -FRAMEWIDTH / 2 + 25;
					}
					else
					{
						vx = 0.0f;
						vy = 1.0f;
						idx = -FRAMEHEIGHT / 2 + 5;
						idy = -FRAMEWIDTH / 2 + 30;
					}
				}
				else
				{
					if (_isDown == 1)
					{
						if (_isMove)
						{
							vx = -1.0f;
							vy = -1.0f;
							idx = -FRAMEHEIGHT + 25;
							idy = -FRAMEWIDTH + 15;
						}
						else
						{
							vx = 0.0f;
							vy = -1.0f;
							idx = -FRAMEHEIGHT / 2 + 5;
							idy = -FRAMEWIDTH / 2 - 15;
						}
					}
					else
					{
						vx = -1.0f;
						vy = 0.0f;
						idx = -FRAMEHEIGHT / 2 - 15;
						idy = -FRAMEWIDTH / 2;
					}
				}
			}
		}
	#pragma endregion
	#pragma region x + iframe
	int iframe = 0;// = _x - SCREEN_WIDTH / 2;
	if (_x < SCREEN_WIDTH / 2)
		iframe = 0;
	else
	{
		if (_x > GAMEWORLDWIDTH - SCREEN_WIDTH / 2)
			iframe = _x - SCREEN_WIDTH + (GAMEWORLDWIDTH - _x);
		else
			iframe = _x - SCREEN_WIDTH / 2;
	}
	#pragma endregion
	#pragma region delete bullet old
	if (_listBullet.size() > 0)
	{
		list<CBullet>::iterator ibullet = _listBullet.begin();
		if ((*ibullet)._levelBullet != _levelBullet)
		{
			_listBullet.clear();
		}
	}
	#pragma endregion

	switch (_levelBullet)
	{
		#pragma region R
		case 1:		//		R
			vx *= 1.2;
			vy *= 1.2;
			break;
#pragma endregion
		#pragma region S
		case 4:		//		S
			_bullet = new CBullet(_x + FRAMEWIDTH - iframe + idx, _y - FRAMEHEIGHT / 2 + idy, BULLETTIMEALIVE, vx * CONTRABULLETVX, vy * CONTRABULLETVY, _levelBullet, CONTRAALPHA);
			Add(_bullet);


			_bullet = new CBullet(_x + FRAMEWIDTH - iframe + idx, _y - FRAMEHEIGHT / 2 + idy, BULLETTIMEALIVE, vx * CONTRABULLETVX, vy * CONTRABULLETVY, _levelBullet, CONTRAALPHA);
			Add(_bullet);

			_bullet = new CBullet(_x + FRAMEWIDTH - iframe + idx, _y - FRAMEHEIGHT / 2 + idy, BULLETTIMEALIVE, vx * CONTRABULLETVX, vy * CONTRABULLETVY, _levelBullet, CONTRAALPHA);
			Add(_bullet);

			_bullet = new CBullet(_x + FRAMEWIDTH - iframe + idx, _y - FRAMEHEIGHT / 2 + idy, BULLETTIMEALIVE, vx * CONTRABULLETVX, vy * CONTRABULLETVY, _levelBullet, CONTRAALPHA);
			Add(_bullet);

			_bullet = new CBullet(_x + FRAMEWIDTH - iframe + idx, _y - FRAMEHEIGHT / 2 + idy, BULLETTIMEALIVE, vx * CONTRABULLETVX, vy * CONTRABULLETVY, _levelBullet, CONTRAALPHA);
			Add(_bullet);
			break;
		#pragma endregion
		#pragma region F
		case 6:		//		F
			if (++_countF < 5)
			{
				_bullet = new CBullet(_x + FRAMEWIDTH - iframe + idx, _y - FRAMEHEIGHT / 2 + idy, 3000.0f, vx * 2, vy * 2, _levelBullet, 0.0f);
				Add(_bullet);
			}
			else
				if (_timerF.ClockTrue())
				{
					_countF = 0;
				}
			break;
		#pragma endregion
		#pragma region L
		case 7:		//		L
			if (GunAddL(1))
			{
				_bullet = new CBullet(_x + FRAMEWIDTH - iframe + idx, _y - FRAMEHEIGHT / 2 + idy, BULLETTIMEALIVE, vx * CONTRABULLETVX, vy * CONTRABULLETVY, _levelBullet, 0.0f);
				Add(_bullet);
				_bullet = new CBullet(_x + FRAMEWIDTH - iframe + idx + 32, _y - FRAMEHEIGHT / 2 + idy, BULLETTIMEALIVE, vx * CONTRABULLETVX, vy * CONTRABULLETVY, _levelBullet, 0.0f);
				Add(_bullet);
				_bullet = new CBullet(_x + FRAMEWIDTH - iframe + idx + 64, _y - FRAMEHEIGHT / 2 + idy, BULLETTIMEALIVE, vx * CONTRABULLETVX, vy * CONTRABULLETVY, _levelBullet, 0.0f);
				Add(_bullet);
			}
			break;
		
		case 8:		//	L2
			{
				  _timerL.SetStartTime();
				if (GunAddL(2))
				{
					_bullet = new CBullet(_x + FRAMEWIDTH - iframe + idx, _y - FRAMEHEIGHT / 2 + idy, BULLETTIMEALIVE, vx * CONTRABULLETVX, vy * CONTRABULLETVY, _levelBullet, 0.0f);
					Add(_bullet);
					_bullet = new CBullet(_x + FRAMEWIDTH - iframe + idx + 32, _y - FRAMEHEIGHT / 2 + idy, BULLETTIMEALIVE, vx * CONTRABULLETVX, vy * CONTRABULLETVY, _levelBullet, 0.0f);
					Add(_bullet);
					_bullet = new CBullet(_x + FRAMEWIDTH - iframe + idx + 64, _y - FRAMEHEIGHT / 2 + idy, BULLETTIMEALIVE, vx * CONTRABULLETVX, vy * CONTRABULLETVY, _levelBullet, 0.0f);
					Add(_bullet);
				}
				else
						if (GunAddL(1))
						{
							_bullet = new CBullet(_x + FRAMEWIDTH - iframe + idx, _y - FRAMEHEIGHT / 2 + idy, BULLETTIMEALIVE, vx * CONTRABULLETVX, vy * CONTRABULLETVY, _levelBullet, 0.0f);
							Add(_bullet);
							_bullet = new CBullet(_x + FRAMEWIDTH - iframe + idx + 32, _y - FRAMEHEIGHT / 2 + idy, BULLETTIMEALIVE, vx * CONTRABULLETVX, vy * CONTRABULLETVY, _levelBullet, 0.0f);
							Add(_bullet);
							_bullet = new CBullet(_x + FRAMEWIDTH - iframe + idx + 64, _y - FRAMEHEIGHT / 2 + idy, BULLETTIMEALIVE, vx * CONTRABULLETVX, vy * CONTRABULLETVY, _levelBullet, 0.0f);
							Add(_bullet);
						}
						break;
			}
			break;
		#pragma endregion
		#pragma region M
		default:	//		M...
			_bullet = new CBullet(_x + FRAMEWIDTH - iframe + idx, _y - FRAMEHEIGHT / 2 + idy, BULLETTIMEALIVE, vx * CONTRABULLETVX, vy * CONTRABULLETVY, _levelBullet, 0.0f);
			Add(_bullet);
			break;
		#pragma endregion
	}
}

//---------------------------------------------------------------------------
//		BULLET: L
//---------------------------------------------------------------------------
bool CGunContra::GunAddL(int iKeyboard)
{
	if (_listBullet.size() > 0)
	{
		switch (iKeyboard)
		{
			case 1:		// to keep J 
			{
							list<CBullet>::iterator ibullet = _listBullet.begin();
							if ((*ibullet)._x > SCREEN_WIDTH || (*ibullet)._y > SCREEN_HEIGHT)
							{
								_listBullet.clear();
								return true;
							}
			}
				break;

			case 2:		// down up, 1000msl
			{
							list<CBullet>::iterator ibullet = _listBullet.begin();
							if (GetTickCount() - (*ibullet)._timeaLive.GetStartTime() < BULLETTIMEALIVE)
							{
								_listBullet.clear();
								return true;
							}
			}
				break;
		}
	}
	else
		return true;
	return false;
}

double CGunContra::Sin(double ialpha)
{
	return sin(ialpha*PI / 180);
}

double CGunContra::Cos(double ialpha)
{
	return cos(ialpha*PI / 180);
}

void CGunContra::Draw(CSprite *isprite)
{
	CheckALiveBullet();

	if (_listBullet.size() > 0)
	{
		list<CBullet>::iterator ibullet = _listBullet.begin();
		for (ibullet; ibullet != _listBullet.end(); ibullet++)
		{
			if ((*ibullet).CheckALive())
			{
				switch ((*ibullet)._levelBullet)
				{
					#pragma region S
					case 4:   // S
					{
								  if ((*ibullet)._timeaLive.SubTime() <= 300)
								  {
									  isprite->Next((*ibullet)._levelBullet - 1, 1);
								  }
								  else
								  {
									  if ((*ibullet)._timeaLive.SubTime() > 300 && (*ibullet)._timeaLive.SubTime() < 600)
									  {
										  isprite->Next((*ibullet)._levelBullet, 1);
									  }
									  else
									  {
										  isprite->Next((*ibullet)._levelBullet + 1, 1);
									  }
								  }

								  (*ibullet).Update();
								  float ix0 = (*ibullet)._x0;		// vi tri ban dau
								  float iy0 = (*ibullet)._y0;
								  float dx = (*ibullet)._x - ix0;	//khoang cach tu vi tri cua dan den vi tri ban dau
								  float dy = (*ibullet)._y - iy0;
								  float ialpha = (*ibullet)._alpha;
								  (*ibullet).Draw(isprite);
								  ibullet++;

								  //Ban ngang
								  /*(*ibullet).SetLocation(ix0 + dx * Cos( ialpha), iy0 + dx*Sin(ialpha));
								  (*ibullet).Draw(isprite);*/

								  //Ban thang
								  /*(*ibullet).SetLocation(ix0 - dy * Sin(ialpha), iy0 + dy*Cos( ialpha));
								  (*ibullet).Draw(isprite);*/

								  // vi tri ban dau - ban ngang(cos,sin) + ban len(sin,cos): chay theo y thi tru theo x thi cong
								  (*ibullet).SetLocation(ix0 - dy * Sin(ialpha) + dx * Cos(ialpha), iy0 + dy*Cos(ialpha) + dx*Sin(ialpha));
								  (*ibullet).Draw(isprite);
								  ibullet++;

								  (*ibullet).SetLocation(ix0 - dy* Sin(2 * ialpha) + dx* Cos(2 * ialpha), iy0 + dy*Cos(2 * ialpha) + dx*Sin(2 * ialpha));
								  (*ibullet).Draw(isprite);
								  ibullet++;

								  (*ibullet).SetLocation(ix0 - dy *Sin(-ialpha) + dx *Cos(-ialpha), iy0 + dy*Cos(-ialpha) + dx*Sin(-ialpha));
								  (*ibullet).Draw(isprite);
								  ibullet++;

								  (*ibullet).SetLocation(ix0 - dy *Sin(-2 * ialpha) + dx *Cos(-2 * ialpha), iy0 + dy*Cos(-2 * ialpha) + dx*Sin(-2 * ialpha));
								  (*ibullet).Draw(isprite);
					}

						break;
					#pragma endregion
					#pragma region F
					case 6:
					{
							  isprite->Next((*ibullet)._levelBullet, 1);
							  float iy0 = (*ibullet)._y0;
							  int a = 30;

							  (*ibullet)._alpha -= 30.0f;
							  int alpha = (*ibullet)._alpha;

							  if (-alpha % 360 >= 90 && -alpha % 360 <= 270)//(-alpha % 360 == 90)
							  {
								  (*ibullet)._x0 += (*ibullet)._vx * 2;//10;
								  (*ibullet)._y0 += (*ibullet)._vy * 2;//10;
							  }
							  /*else
							  {
							  (*ibullet)._x0 += (*ibullet)._vx;
							  (*ibullet)._y0 += (*ibullet)._vy;
							  }*/

							  (*ibullet).SetLocation((*ibullet)._x0 - a* Sin((*ibullet)._alpha), iy0 + a*Cos((*ibullet)._alpha));// (x, iy0 + a * Sin(_beta));
							  (*ibullet).Draw(isprite);
					}

						break;
					#pragma endregion
					#pragma region L
					case 7:
					{
							  isprite->Next(0, 1);
							  (*ibullet).Update();
							  (*ibullet).Draw(isprite);
							  ibullet++;

							  (*ibullet).Update();
							  (*ibullet).Draw(isprite);
							  ibullet++;

							  (*ibullet).Update();
							  (*ibullet).Draw(isprite);
					}

						break;

					case 8:
						isprite->Next(0, 1);
						if (_timerL.SubTime()>=120)
						{
							(*ibullet)._levelBullet = 7;
							ibullet++;
							(*ibullet)._levelBullet = 7;
							ibullet++;
							(*ibullet)._levelBullet = 7;
						}
						else
						{
							if ((int)_timerL.SubTime() % 200 <= 30)
							{
								(*ibullet).Draw(isprite);
								ibullet++;
								ibullet++;
							}
							else
							{
								if ((int)_timerL.SubTime() > 30 && (int)_timerL.SubTime() <= 70)
								{
									(*ibullet).Draw(isprite);
									ibullet++;

									(*ibullet).Draw(isprite);
									ibullet++;
								}
								else
								{
									(*ibullet).Draw(isprite);
									ibullet++;

									(*ibullet).Draw(isprite);
									ibullet++;

									(*ibullet).Draw(isprite);
								}
							}
						}
						break;
					#pragma endregion
					#pragma region M
					default:
					{
							   isprite->Next((*ibullet)._levelBullet, 1);
							   (*ibullet).Update();
							   (*ibullet).Draw(isprite);
					}

						break;
					#pragma endregion
				}
			}
		}
	}
}

void CGunContra::GunContra_End()
{
	if (_bullet != NULL)
		delete _bullet;

	if (_sprite != NULL)
		delete _sprite;
}

CGunContra::~CGunContra()
{
}
